﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WinChat02
{
    public partial class Form1 : Form
    {
        private Button ClientButton;
        public TextBox Chat;
        public TextBox Msg;
        public Label label1;
        private Button ServerButton;
        public TextBox Log;

        public nw_core NetCore;
        private Timer timer1;
        private IContainer components;
        private Timer timer2;

        private System.Object lockThis = new System.Object();

        public Form1()
        {
            InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;

        }

        public string ChatText
        {
            get { return Chat.Text; }
            set { Chat.Text = value; }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            NetCore = new nw_core(this);
            LogAdd("Starting application...");

        }
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.ServerButton = new System.Windows.Forms.Button();
            this.ClientButton = new System.Windows.Forms.Button();
            this.Chat = new System.Windows.Forms.TextBox();
            this.Msg = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.Log = new System.Windows.Forms.TextBox();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.timer2 = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            // 
            // ServerButton
            // 
            this.ServerButton.Location = new System.Drawing.Point(17, 17);
            this.ServerButton.Name = "ServerButton";
            this.ServerButton.Size = new System.Drawing.Size(135, 36);
            this.ServerButton.TabIndex = 0;
            this.ServerButton.Text = "Server";
            this.ServerButton.UseVisualStyleBackColor = true;
            this.ServerButton.Click += new System.EventHandler(this.ServerButton_Click);
            // 
            // ClientButton
            // 
            this.ClientButton.Location = new System.Drawing.Point(17, 76);
            this.ClientButton.Name = "ClientButton";
            this.ClientButton.Size = new System.Drawing.Size(134, 36);
            this.ClientButton.TabIndex = 1;
            this.ClientButton.Text = "Client";
            this.ClientButton.UseVisualStyleBackColor = true;
            this.ClientButton.Click += new System.EventHandler(this.ClientButton_Click);
            // 
            // Chat
            // 
            this.Chat.Location = new System.Drawing.Point(158, 17);
            this.Chat.Multiline = true;
            this.Chat.Name = "Chat";
            this.Chat.Size = new System.Drawing.Size(866, 435);
            this.Chat.TabIndex = 2;
            this.Chat.Visible = false;
            this.Chat.TextChanged += new System.EventHandler(this.Chat_TextChanged);
            // 
            // Msg
            // 
            this.Msg.Location = new System.Drawing.Point(158, 450);
            this.Msg.Multiline = true;
            this.Msg.Name = "Msg";
            this.Msg.Size = new System.Drawing.Size(866, 105);
            this.Msg.TabIndex = 3;
            this.Msg.Visible = false;
            this.Msg.TextChanged += new System.EventHandler(this.Msg_TextChanged);
            this.Msg.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Msg_KeyDown);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(59, 195);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(35, 13);
            this.label1.TabIndex = 4;
            this.label1.Text = "label1";
            // 
            // Log
            // 
            this.Log.BackColor = System.Drawing.Color.Black;
            this.Log.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
            this.Log.ForeColor = System.Drawing.Color.White;
            this.Log.Location = new System.Drawing.Point(158, 561);
            this.Log.Multiline = true;
            this.Log.Name = "Log";
            this.Log.Size = new System.Drawing.Size(866, 187);
            this.Log.TabIndex = 5;
            // 
            // timer1
            // 
            this.timer1.Enabled = true;
            this.timer1.Interval = 50;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // timer2
            // 
            this.timer2.Enabled = true;
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(1027, 751);
            this.Controls.Add(this.Log);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.Msg);
            this.Controls.Add(this.Chat);
            this.Controls.Add(this.ClientButton);
            this.Controls.Add(this.ServerButton);
            this.Name = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void ServerButton_Click(object sender, EventArgs e)
        {
            NetCore.StartChat(ConnectionRole.CRServer);
            ClientButton.Visible = false;
            ServerButton.Visible = false;
            label1.Text = "Started as Server";
        }

        private void ClientButton_Click(object sender, EventArgs e)
        {
            NetCore.StartChat(ConnectionRole.CRClient);
            ServerButton.Visible = false;
            ClientButton.Visible = false;
            label1.Text = "Started as client";
        }

        public void LogAdd(string s)
        {

            Log.Text += (System.Environment.NewLine + s);
        }

        private void Msg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                NetCore.SendMsg(Msg.Text);
                Chat.Text += "I'AM:" + Msg.Text + Environment.NewLine;
                Msg.Text = "";
            }
        }

        private void Msg_TextChanged(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //if (NetCore != null)
            //    if (NetCore.Role == ConnectionRole.CRServer)
            //    {
            //        if (NetCore.server.handler != null)
            //            if (NetCore.server.handler.Connected)
            //            {
            //                NetCore.server.ReceiveThread();
            //            }
            //    }

            //if (NetCore != null)
            //if (NetCore.Role == ConnectionRole.CRClient)
            //{
            //    if (NetCore.client != null)
            //        if (NetCore.client.client_socket.Connected)
            //        {
            //            NetCore.client.ReceiveThread();
            //        }
            //}

            if (NetCore != null & NetCore.Role != ConnectionRole.CRNone)
            {

                if (NetCore.Role == ConnectionRole.CRClient)
                    if (NetCore.client.client_socket != null)
                        if (NetCore.client.client_socket.Connected)
                        {
                            NetCore.ReceiveThreadMsg();
                        }

                if (NetCore.Role == ConnectionRole.CRServer)
                    if (NetCore.server.handler != null)
                        if (NetCore.server.handler.Connected)
                        {
                            NetCore.ReceiveThreadMsg();
                            
                            SetVisibility(true);
                            
                        }


            }

        }

        public void SetVisibility(bool b) 
        {
            Chat.Visible = b;
            Msg.Visible = b;
            this.Refresh();

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (NetCore != null)
                if (NetCore.Role == ConnectionRole.CRClient)
                    if (NetCore.client.client_socket != null)
                        if (NetCore.client.client_socket.Connected)
                        {
                            NetCore.SendMsg("!exit");
                        }
        }

        private void Chat_TextChanged(object sender, EventArgs e)
        {

        }

    }
}